package com.boge.core.serviceImpl;

import javax.annotation.Resource;

import com.boge.core.model.dao.mappers.UserLockMapper;
import com.boge.core.model.dao.mappers.UserMapper;
import com.boge.core.model.entity.UserLock;
import com.boge.core.service.UserLockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserLockServiceImpl implements UserLockService {

	@Autowired
	UserLockMapper userLockMapper;
	
	@Resource
	UserMapper userMapper;
	
	@Override
	public int getUserLockCountsByUserId(int userId) {
		UserLock userLock = userLockMapper.getUserLockByUserId(userId);
		System.out.println("userLock:" + userLock);
		if (userLock == null) {
			return 0;
		}
		return userLock.getUserLockCount();
	}
	
	@Override
	public boolean cleanUserLock() {
		int count = userLockMapper.cleanUserLock();
		System.out.println("count:" + count);
		return true;
	}
	
	/**
	 * 增加用户锁次数，如果没有，则创建之
	 */
	@Override
	public boolean raiseUserLockCountByUserId(int userId) {
		UserLock userLock = userLockMapper.getUserLockByUserId(userId);
		if (userLock == null) {
			userLock = new UserLock();
			userLock.setUserLockCount(1);
			userLock.setUser(userMapper.getUserByUserId(userId));
			int count = userLockMapper.addUserLock(userLock);
			System.out.println("addUserLock--id:" + count);
			if (count > 0) {
				return true;
			}
		}
		
		if (userLock.getUserLockCount() == 3) {
			return true;
		}
		
		int id = userLockMapper.raiseUserLockCountByUserId(userId);
		if (id < 0) {
			return false;
		}
		
		return true;
	}
	
	@Override
	public boolean deleteUserLockByUserId(int userId) {
		int count = userLockMapper.deleteUserLockByUserId(userId);
		System.out.println("deleteUserLockNum:" + count);
		return true;
	}
	
}
